/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import database.Database;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Dana
 */
@WebServlet(name = "LoginServlet", urlPatterns = {"/loginhandler"})
public class LoginServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
        PrintWriter out = response.getWriter();
        String username = request.getParameter("username");

        String password = request.getParameter("password");
        out.println(password);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String username = request.getParameter("username");

        String password = request.getParameter("password");
        try {
            password = new BigInteger(1, MessageDigest.getInstance("md5").digest(request.getParameter("password").getBytes())).toString(16);
        } catch (NoSuchAlgorithmException ex) {
            out.print("failed to hash the password.");
            return;
        }
        Database database = new Database();
        try {
            if (!database.isUserExist(username)) {
                out.print("username_not_exist");
                return;
            }
        } catch (SQLException ex) {
            out.print("failed to check is user exist.");
            return;
        }
        try {
            if (!database.getPasswordUser(username).equals(password)) {
                out.print("wrong_password");
                return;
            }
        } catch (SQLException ex) {
            out.print("failed to get user's password");
            return;
        }
        HttpSession session = request.getSession();
        session.setAttribute("token", username);
        out.print("success");
        return;
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
